import Vue from 'vue'
import Router from 'vue-router'
import Layout from '../views/layout/Layout' // 主容器组件
import { isLogin } from '@/api/login'
const _import = require('./_import_' + process.env.NODE_ENV + '.js') // 加载路由方法（这样就不需要申明了）

Vue.use(Router)

export const constantRouterMap = [ // 公共路由
  { path: '', redirect: '/login' },
  { path: '/login', component: _import('login/login'), hidden: true }, // huidden 用来表示是否显示在左侧主导航上
  { path: '',
    component: Layout,
    children: [{
      path: 'index',
      component: _import('index/index'),
      name: 'index',
      icon: 'index',
      title: '主页'
    }]
  },
  {
    path: '*',
    component: Layout,
    redirect: 'login'
  }
]

let myRouter = new Router({
  mode: 'history',
  routes: constantRouterMap
})
// 判断是否登录
myRouter.beforeEach((to, from, next) => {
  isLogin().then(res => {
    if (res.code === 1) {
      next()
    } else {
      next('/login')
    }
  })
})

export default myRouter
